package cn.lxycx.dataterrace.interceptor;

import javax.servlet.http.HttpServletRequest;

import lxycx.util.run.ServletPro;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 用户操作日志
 * @author 邢超
 * 时间：2017年9月15日
 */
public class UserLogIntercept implements Interceptor {
	public static Prop prop = PropKit.use("req.properties");
	private static Logger log = Logger.getLogger(UserLogIntercept.class);
	private String driver = PropKit.get("driver");
	
	@Override
	public void intercept(Invocation inv) {
		Controller con = inv.getController();
		HttpServletRequest req = con.getRequest();
		String path = req.getServletPath();
		String explain = prop.get(path);
		String ip = ServletPro.getIP(req);
		JSONObject parajson = JSONObject.fromObject(con.getParaMap());
		
		String account = "",username = "";
		Record user = con.getSessionAttr("user");
		if(user!=null){
			account = user.getStr("account");
			username = user.getStr("username");
		}
		
		String params = parajson.toString();

		Record record = new Record().set("account", account).set("username", username).set("ip", ip)
				.set("sceneurl", path).set("scenename", explain).set("params",params);

		if(driver.indexOf("oracle")>0){//oracle
			record.set("id", "data_statement_actionlog_seq.nextval");
		}

		Db.save("data_statement_actionlog", record);
		
		log.debug("path:"+path+"---explain:"+explain);
		inv.invoke();
	}

}